package com.logic.modular.gameCore.match.battle.service.impl;

import com.comment.pojo.domain.logic.core.CoreSkillEffect;
import com.logic.modular.gameCore.match.battle.mapper.CoreSkillEffectMapper;
import com.logic.modular.gameCore.match.battle.service.CoreSkillEffectService;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreSkillEffect)表服务实现类
 *
 * @author cxy
 * @since 2024-08-21 18:04:41
 */
@Slf4j
@Service
public class CoreSkillEffectServiceImpl extends ServiceImpl<CoreSkillEffectMapper, CoreSkillEffect> implements CoreSkillEffectService {
    Cache<String, Map<Long,CoreSkillEffect>> cache = Caffeine.newBuilder().build();
    @PostConstruct
    private void init() {
        CoreSkillEffect initSelect = CoreSkillEffect.builder().build();
        List<CoreSkillEffect> list = this.list(new LambdaQueryWrapper<>(initSelect));
        cache.put(
                "coreSkillEffect",
                list.stream().collect(Collectors.toMap(CoreSkillEffect::getId, CoreSkillEffect::init ))
        );
        log.info("缓存技能效果基础数据成功,数据量为:{}",list.size());
    }

    public List<CoreSkillEffect> listCache() {
        Map<Long,CoreSkillEffect> values = cache.getIfPresent("coreSkillEffect");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return new ArrayList<>(values.values());
    }     
    
    public Map<Long,CoreSkillEffect> mapCache() {
        Map<Long,CoreSkillEffect> values = cache.getIfPresent("coreSkillEffect");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }   
    
    public CoreSkillEffect getCache(Long id) {
        Map<Long,CoreSkillEffect> values = cache.getIfPresent("coreSkillEffect");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values.get(id);
    }
}
